package nz.ac.massey.cs.se400_2013.ass3;

import static org.junit.Assert.*;

import java.util.Collection;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestExtendedObjectStore {
       
    /**
         * @author jmcurtis
         */
       
		private static  ExtendedObjectStore store;
		
		@BeforeClass
		public static void setup(){
			store = ExtendedObjectStore.getInstance();
			
            
            // adds stuff to test
            Course course = new Course();
            course.setName("MSc");
           
            Student jim = new Student();
            jim.setName("Curtis");
            jim.setFirstName("James");
           
            Paper paper = new Paper();
            paper.setId("42");
            paper.setName("paper");
            
            Paper paper2 = new Paper();
            paper.setId("43");
            paper.setName("paper");
           
            course.addPaper(paper);
           
            Address address = new Address();
            address.setStreet("Surf Street");
            address.setCity("Palmerston North");
           
            jim.setAddress(address);
            jim.addPaper(paper);
            jim.setCourse(course);

            store.COURSES.add(course);
		}
	
			@Test
			public void testExistance(){
				assertTrue(store != null);
			}
			
			@Test
			//ensure that getInstance always returns the same instance
			public void testSame(){
				ExtendedObjectStore first = ExtendedObjectStore.getInstance();
				ExtendedObjectStore second = ExtendedObjectStore.getInstance();
				assertEquals(first, second);
			}
			
			@Test
			public void containsJames(){
				Collection<Student> students = store.getStudents();
				for(Student student : students){
					if(student.getFirstName().equals("james")){
						assertTrue(true);
					}
				}
			}
			
			@Test
			public void containsPaper1(){
				Collection<Paper> papers = store.getPapers();
				for(Paper paper : papers){
					if(paper.getId() == ("42")){
						assertTrue(true);
					}
				}
			}
			
			@Test
			public void containsPaper2(){
				Collection<Paper> papers = store.getPapers();
				for(Paper paper : papers){
					if(paper.getId() == ("43")){
						assertTrue(true);
					}
				}
			}
			
			@Test
			//if the method ever ends without exception then recursion is taken care of
			public void recursion(){
				Student student = new Student();
				Paper paper = new Paper();
				Course course = new Course();
				
				//getting this far means recursion wasnt an issue.
				assertTrue(true);
				
				student.addPaper(paper);
				assertTrue(student.getPapers().contains(paper));
				assertTrue(paper.getEnrolledStudents().contains(student));
				
				paper.addCourse(course);
				assertTrue(course.getPapers().contains(paper));
				assertTrue(paper.getCourses().contains(course));
				
			}

			
}

